using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace FileServerDocumenter
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = Application.ProductName;
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            saveFileDialog1.DefaultExt = "csv";
            saveFileDialog1.Filter = "Character Seperated Values (*.csv)|*.csv";

            saveFileDialog1.ShowDialog();
            txtOutputPath.Text = saveFileDialog1.FileName;
        }

        private void btnSetFileSecurityOutput_Click(object sender, EventArgs e)
        {
            saveFileDialog1.DefaultExt = "csv";
            saveFileDialog1.Filter = "Character Seperated Values (*.csv)|*.csv";

            saveFileDialog1.ShowDialog();
            txtSecurityOutputPath.Text = saveFileDialog1.FileName;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            if (IsValidForm())
            {
                PerformDocumentation();

                if (chkOpenOutput.Checked == true)
                {
                    //shell out the output path
                    Process p = new Process();
                    p.StartInfo.FileName = txtOutputPath.Text;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();

                    //shell out the output path
                    p = new Process();
                    p.StartInfo.FileName = txtSecurityOutputPath.Text;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                }
            }
        }

        bool IsValidForm()
        {
            if (radServerShares.Checked == false & radSubDirectories.Checked == false)
            {
                MessageBox.Show("Please select a type of target location");
                return false;
            }

            if (radServerShares.Checked == true & txtServerName.Text == "")
            {
                MessageBox.Show("Please enter a server name");
                return false;
            }

            if (radSubDirectories.Checked == true & txtPath.Text == "")
            {
                MessageBox.Show("Please enter a directory path");
                return false;
            }

            if (radDocumentDirectories.Checked == false & radDocumentFiles.Checked == false)
            {
                MessageBox.Show("Please select a documentation activity");
                return false;
            }

            if (txtOutputPath.Text == "")
            {
                MessageBox.Show("Please enter or select an output path");
                return false;
            }

            if (chkIncludeSecurity.Checked == true & txtSecurityOutputPath.Text == "")
            {
                MessageBox.Show("Since you have selected to document security, please enter or select a security output path");
                return false;
            }

            return true;
        }


        private void PerformDocumentation()
        {
            if (radDocumentDirectories.Checked == true)
            {
                if (radServerShares.Checked == true)
                {
                    Program.DocumentShares(txtServerName.Text, txtOutputPath.Text, txtSecurityOutputPath.Text, 
                            chkIncludeDirectorySize.Checked, chkIncludeSecurity.Checked);
                }
                else
                {
                    Program.DocumentSubDirectories(txtPath.Text, txtOutputPath.Text, txtSecurityOutputPath.Text,
                            chkIncludeDirectorySize.Checked, chkIncludeSecurity.Checked);
                }
            }
            else
            {
                if (radServerShares.Checked)
                {
                    Program.DocumentFiles(txtServerName.Text, txtExtension.Text, radServerShares.Checked);
                }
                else
                {
                    Program.DocumentFiles(txtPath.Text, txtExtension.Text, radServerShares.Checked);
                }
            }
        }
    }
}